/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//adss
package testeinterface;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class PDisciplina extends JPanel{       
        
        public PDisciplina(){            
            this.setLayout(new GridLayout(5,2));
            
          final JTextField tfSigla        = new JTextField("",20);
          final JTextField  tfNome        = new JTextField("",20);
          final JTextField tfCreditos     = new JTextField("",20);
          final JTextField tfAno          = new JTextField("",20);
          JButton  btInserir    = new JButton("Inserir");
          JLabel  lbSigla       = new JLabel("Sigla da disciplina");
          JLabel lbNome         = new JLabel("Nome da disciplina");
          JLabel  lbCreditos    = new JLabel("Número de creditos");
          JLabel  lbAno         = new JLabel("Ano do catálogo");
            
            btInserir.addActionListener (new ActionListener () {  
            @Override
                 public void  actionPerformed(ActionEvent e){  
                     
                     Logger.gravar(Logger.INICIALIZANDO_METODO, getNomeClasse() + ".actionPerformed(ActionEvent e)");
                     
                     try{
                        String host = "jdbc:mysql://143.106.150.216:3306/SatodaDB?zeroDateTimeBehavior=convertToNull";
                        String uName = "satoda";
                        String uPass = "adotas2014";
                        Connection con = DriverManager.getConnection(host, uName, uPass);
                        
                        Statement stmt = con.createStatement( );
                        String SQL = "INSERT INTO T_DISCIPLINA(sigla, nome, creditos, anoCatalogo) VALUES ('"+ tfSigla.getText() +"', '"+ tfNome.getText() +"', "+tfCreditos.getText()+", "+tfAno.getText()+")";
                        stmt.executeUpdate( SQL );
                    }
                    catch( SQLException err ){
                        Logger.gravar(Logger.ERRO, getNomeClasse() + ".actionPerformed(ActionEvent e): " + err.getMessage());
                        System.out.println(err.getMessage());
                    }
                     
                    Logger.gravar(Logger.FINALIZANDO_METODO, getNomeClasse() + ".actionPerformed(ActionEvent e)");
                    System.out.println("Disciplina cadastrada com sucesso!");  
                 }  
              });  
            
            this.add(lbSigla);
            this.add(tfSigla);
            
            this.add(lbNome);
            this.add(tfNome);
            
            this.add(lbCreditos);
            this.add(tfCreditos);
            
            this.add(lbAno);
            this.add(tfAno);
            
            this.add(btInserir);
        }
        
        private String getNomeClasse(){
            return this.getClass().getName();
        }
}

